package com.example.guli.service.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.guli.common.base.result.R;
import com.example.guli.service.edu.entity.vo.TeacherQueryVo;
import com.example.guli.service.edu.entity.Course;
import com.example.guli.service.edu.entity.Teacher;
import com.example.guli.service.edu.feign.OssFileService;
import com.example.guli.service.edu.mapper.CourseMapper;
import com.example.guli.service.edu.mapper.TeacherMapper;
import com.example.guli.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author shunxuan
 * @since 2020-07-16
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {
    @Autowired
    private CourseMapper courseMapper;

    @Override
    public IPage<Teacher> selectPage(Page<Teacher> pageParam, TeacherQueryVo teacherQueryVo) {
        //显示分页查询列表
         //1.排序：按照sort字段处理
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("sort");

        //2.分页查询
        if(teacherQueryVo==null) {
         return  baseMapper.selectPage(pageParam, queryWrapper);
        }

        //3.条件查询
        String name = teacherQueryVo.getName();
        Integer level = teacherQueryVo.getLevel();
        String joinDateBegin = teacherQueryVo.getJoinDateBegin();
        String joinDateEnd = teacherQueryVo.getJoinDateEnd();
        if (!StringUtils.isEmpty(name)) {
            //左%会使索引失效
            queryWrapper.likeRight("name", name);
        }
        if (level != null) {
            //筛选
            queryWrapper.eq("level", level);
        }
        //入驻时间
        if (!StringUtils.isEmpty(joinDateBegin)) {
            //大于等于
            queryWrapper.ge("join_date", joinDateBegin);
        }
        if (!StringUtils.isEmpty(joinDateEnd)) {
            //小于等于
            queryWrapper.le("join_date", joinDateEnd);
        }
        return baseMapper.selectPage(pageParam,queryWrapper);
    }

    @Override
    public List<Map<String, Object>> selectNameList(String key) {
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        //搜索关键字，只显示name字段
        queryWrapper.select("name");
        //模糊查询
        queryWrapper.likeRight("name",key);
        //查询指定的字段，过滤为null的值，必须使用Map集合才能实现
        List<Map<String, Object>> list = baseMapper.selectMaps(queryWrapper);
        return list;
    }

    @Autowired//依赖注入oss服务调用的OssFileService接口
    private OssFileService ossFileService;
    @Override
    public boolean removeAvatarById(String id) {
//        获取讲师，找到讲师的头像再删除
        Teacher teacher = baseMapper.selectById(id);
        if(teacher != null) {
            String avatar = teacher.getAvatar();
            if(!StringUtils.isEmpty(avatar)){
                //删除图片
                R r = ossFileService.removeFile(avatar);
                return r.getSuccess();
            }
        }
        return false;
    }


    /**
     * 根据讲师id获取讲师详情页数据
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> selectTeacherInfoById(String id) {
//        获取讲师信息
        Teacher teacher = baseMapper.selectById(id);
//        根据讲师id获取讲师课程
        List<Course> courselist = courseMapper.selectList(new QueryWrapper<Course>());
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("teacher",teacher);
        hashMap.put("courseList",courselist);
        return hashMap;
    }

    @Override
    public List<Teacher> selectHotTeacher() {
        QueryWrapper<Teacher> teacherQueryWrapper = new QueryWrapper<>();
        teacherQueryWrapper.orderByAsc("sort");
        teacherQueryWrapper.last("limit 4");
        return baseMapper.selectList(teacherQueryWrapper);

    }



}
